package org.jet.emall.prod.transfer;


import org.jet.emall.prod.dto.NewOrderDTO;
import org.jet.emall.prod.dto.NewOrderItemDTO;
import org.jet.emall.prod.dto.OrderDTO;
import org.jet.emall.prod.dto.OrderItemDetailViewDTO;
import org.jet.emall.prod.model.NewOrderItemModel;
import org.jet.emall.prod.model.NewOrderModel;
import org.jet.emall.prod.vo.OrderItemDetailViewVO;
import org.jet.emall.prod.vo.OrderVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.LinkedList;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-25 12:36
 */
@Mapper
public interface OrderVoTransfer {

    OrderVoTransfer INSTANCE = Mappers.getMapper(OrderVoTransfer.class) ;


    /**
     *  将新增订单Model转DTO
     * @param model
     * @return
     */
    default NewOrderDTO toDto(NewOrderModel model){
        NewOrderDTO dto = toDto1(model);
        dto.setItems(toItemDtoList(model.getItems()));
        return dto;
    }

    /**
     *    将新增订单项Model转DTO
     * @param models
     * @return
     */
    List<NewOrderItemDTO> toItemDtoList(List<NewOrderItemModel> models);




    /**
     *   ...
     * @param model
     * @return
     */
    @Mapping(target = "items",ignore = true)
    NewOrderDTO toDto1(NewOrderModel model);

    /**
     *
     * @param dtoList
     * @return
     */
    default List<OrderVO> toVoList(List<OrderDTO> dtoList){
        List<OrderVO> voList = new LinkedList<>();
        for(OrderDTO dto : dtoList){
            OrderVO vo = toVo(dto);
            voList.add(vo);
            vo.setItems(toItemVoList(dto.getItems()));
        }
        return voList;
    }


    /**
     *
     * @param items
     * @return
     */
    List<OrderItemDetailViewVO> toItemVoList(List<OrderItemDetailViewDTO> items);


    /**
     *
     * @param dto
     * @return
     */
    @Mapping(target = "items",ignore = true)
    OrderVO toVo(OrderDTO dto);

}
